﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/* Cada instancia de jqGridData contiene una estructura como la siguiente:
    var data = new
    {
        total = totalPages,
        page = currentPage,
        records = totalRecords,
        rows =  from Guion guion in guiones
                select new
                {
                    id = guion.Id,
                    cell = new string[]
                    {
                        guion.Id.ToString(),                            //Id
                        Formatter.Text.ReducirTexto(guion.Titulo, 70) //Título
                    }
                }
    };
*/
namespace Web.Helpers.jqGridHelpers
{
    /// <summary>
    /// Helper class to assist building jqGrid Data
    /// </summary>
    public class jqGridData
    {
        #region Constructors

        /// <summary>
        /// Constructs a jqGridData object with given parameters
        /// </summary>
        /// <param name="totalRecords"></param>
        /// <param name="totalPages"></param>
        /// <param name="currentPage"></param>
        /// <param name="rows"></param>
        public jqGridData(int totalRecords, int totalPages, int currentPage, IEnumerable<object[]> rows)
        {
            this.records = totalRecords;
            this.total = totalPages;
            this.page = currentPage;
            this.rows = new List<jqGridDataRow>();
            if ( rows != null && rows.Count() > 0 )
            {
                foreach (object[] cells in rows)
                {
                    this.rows.Add(new jqGridDataRow(cells));
                }
            }
        }

        #endregion

        #region Properties

        private int _TotalRecords;
        /// <summary>
        /// Total Records
        /// </summary>
        public int records
        {
            get { return _TotalRecords; }
            set { _TotalRecords = value; }
        }

        private int _TotalPages;
        /// <summary>
        /// Total Pages
        /// </summary>
        public int total
        {
            get { return _TotalPages; }
            set { _TotalPages = value; }
        }

        private int _CurrentPage;
        /// <summary>
        /// Current Page
        /// </summary>
        public int page
        {
            get { return _CurrentPage; }
            set { _CurrentPage = value; }
        }

        private List<jqGridDataRow> _Rows;
        /// <summary>
        /// Rows
        /// </summary>
        public List<jqGridDataRow> rows
        {
            get { return _Rows; }
            set { _Rows = value; }
        }
        
        #endregion

        #region Internal Classes 

        /// <summary>
        /// Class for holding jqGridData rows structure
        /// </summary>
        public class jqGridDataRow
        {
            /// <summary>
            /// 
            /// </summary>
            /// <param name="cells"></param>
            public jqGridDataRow(object[] cells)
            {
                this.id     = Convert.ToInt32(cells[0].ToString());
                this.cell   = cells;
            }

            private int _Id;
            /// <summary>
            /// Id
            /// </summary>
            public int id
            {
                get { return _Id; }
                set { _Id = value; }
            }

            private object[] _Cell;
            /// <summary>
            /// Cell
            /// </summary>
            public object[] cell
            {
                get { return _Cell; }
                set { _Cell = value; }
            }
        }

        #endregion
    }
}
